//
//  ZTY_NetHander.m
//  About_Travel
//
//  Created by 赵佟越 on 15/10/21.
//  Copyright © 2015年 赵佟越. All rights reserved.
//

#import "ZTY_NetHander.h"
#import "AFNetworking.h"

@implementation ZTY_NetHander
+ (void)requestGETWithURL:(NSString *)urlStr
                   cookie:(NSString *)cookieStr
               completion:(JSONBLOCK)block
{
    
    // 1.创建网络连接管理器
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2.中文转码
    NSString *newURLStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    // 3.设置返回数据的格式
    //    [manager.requestSerializer setValue:@"c6d5822e76c811e596e85254007d4448" forHTTPHeaderField:@"HTTP-AUTHORIZATION"];
    
    if ([cookieStr isEqualToString:@"shujubuggengzheng"]) {
        
    }else{
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    }
    if (cookieStr) {
        // 4.设置Cookie值
        [manager.requestSerializer setValue:cookieStr forHTTPHeaderField:@"Cookie"];
    }
    
    
    [manager.requestSerializer setValue:@"BreadTrip/android/6.1.2/zh (android OS5.0.1) mx2 Map/GoogleMap/v1 (M040,Meizu) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
        // 5.请求数据
        [manager GET:newURLStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            // 6.请求成功,返回请求到的数据,同时在这里做本地缓存
            // 缓存的文件夹
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
            // 存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            // 归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
            block(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            // 请求失败,读取本地缓存
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
            
            block(result);
            NSLog(@"数据请求失败: %@", error);
        }];
}

+ (void)requestPOSTWithURL:(NSString *)urlStr
                    cookie:(NSString *)cookieStr
                bodyParame:(id)body
                completion:(JSONBLOCK)block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setValue:cookieStr forHTTPHeaderField:cookieStr];
    [manager.requestSerializer setValue:cookieStr forHTTPHeaderField:@"$Version=1"];
    
    // 3.设置返回数据的格式
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",@"application/x-javascript", nil]];
    // 4.设置Cookie值(如果没有,跳过)
    if (cookieStr) {
        [manager.requestSerializer setValue:cookieStr forHTTPHeaderField:@"Cookie"];
    }
    [manager POST:urlStr parameters:body success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        //        NSLog(@"%@", responseObject);
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        // 请求失败,读取本地缓存
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        
        block(result);
        NSLog(@"数据请求失败: %@", error);
    }];
}



+ (void)networkReachabilityStatus:(JSONBLOCK)block
{
    // 网络连接检测封装
    AFNetworkReachabilityManager *reachManeger = [AFNetworkReachabilityManager sharedManager];
    // 开始检测
    [reachManeger startMonitoring];
    
    [reachManeger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                block(@0);
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                block(@1);
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                block(@2);
                break;
            case AFNetworkReachabilityStatusUnknown:
                block(@(-1));
                break;
            default:
                break;
        }
    }];
    //        [reachManeger stopMonitoring];
    
}






@end
